package com.tiancheng.onecode.service.impl;

import cn.hutool.core.util.EnumUtil;
import com.tiancheng.onecode.model.request.MessageBody;
import com.tiancheng.onecode.service.*;
import com.tiancheng.onecode.trade.enums.MessageTypeEnum;
import com.tiancheng.onecode.trade.model.request.TradeMerchantStatusReq;
import com.tiancheng.onecode.trade.notify.CallbackSettleMsgBO;
import com.tiancheng.onecode.trade.notify.PayResultOrderBO;
import com.tiancheng.onecode.trade.notify.RefundResultMsgBO;
import com.tiancheng.onecode.trade.utils.MessageParsing;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @Author: kellen
 * @Description: 订单通知-实现
 * @Date: create in 2024/12/26 16:42
 */
@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {

    @Resource
    private MessageParsing messageParsing;
    @Resource
    private IMemberOrderService memberOrderService;
    @Resource
    private IMemberOrderRefundService memberOrderRefundService;
    @Resource
    private IMerchantService merchantService;
    @Resource
    private IBillService billService;

    @Override
    public void orderNotify(MessageBody messageBody) {
        // 校验签名
        MessageBody msgBody = messageParsing.checkMessage(messageBody);
        String messageType = msgBody.getMessageType();
        if (StringUtils.isEmpty(messageType)) {
            throw new RuntimeException("未查询到指定消息类型");
        }

        final Optional<MessageTypeEnum> messageTypeEnum = MessageTypeEnum.getByCode(messageType);
        if (messageTypeEnum.isEmpty()) {
            throw new RuntimeException("未查询到指定消息类型");
        }
        MessageTypeEnum msgType = messageTypeEnum.
                get();
        switch (msgType){
            case order_pay_result:
                // 支付通知
                log.info("开始处理支付结果消息：{}",msgBody);
                final PayResultOrderBO payResultOrderBO = messageParsing.getData(msgBody, PayResultOrderBO.class);
                memberOrderService.payResultHandler(payResultOrderBO);
                break;
            case order_refund_result:
                // 退款通知
                log.info("开始处理退款结果消息：{}",msgBody);
                final RefundResultMsgBO refundResultMsgBO = messageParsing.getData(msgBody, RefundResultMsgBO.class);
                memberOrderRefundService.refundResultHandler(refundResultMsgBO);
                break;
            case merchant_status:
                log.info("商户状态变更消息：{}",msgBody);
                final TradeMerchantStatusReq merchantStatusReq = messageParsing.getData(msgBody, TradeMerchantStatusReq.class);
                merchantService.merchantStatusChangeNotify(merchantStatusReq);
                break;
            case trade_payment_settle_result:
            case trade_payment_refund_settle_result:
                // 结算相关的消息通知
                CallbackSettleMsgBO settleMsgBO = messageParsing.getData(msgBody, CallbackSettleMsgBO.class);
                billService.settleResultHandler(settleMsgBO,msgType);
                break;

        }
    }

    @Override
    public void merchantNotify(TradeMerchantStatusReq tradeMerchantStatusReq) {
        merchantService.merchantStatusChangeNotify(tradeMerchantStatusReq);
    }
}
